package com.cn.ccp.action.classschedule.user;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.ClassScheduleBean;
import com.cn.ccp.bean.GradeBean;
import com.cn.ccp.bean.TermSettingBean;
import com.cn.ccp.util.CCPContants;

/**
 * 老师课程表
 * @author renyongcheng
 *
 */
public class ClassScheduleTeacher extends BaseAction{
	private Logger log = Logger.getLogger(ClassScheduleTeacher.class);
	private List<ClassScheduleBean> classScheduleList;
	private ClassScheduleBean classScheduleBean;
	private SelectItem[] termSelectItem ;// 学期下拉框值
	
	public ClassScheduleTeacher(){
		initTerm();
	}
	
	private void initTerm(){
		int curYear = Calendar.getInstance().get(Calendar.YEAR);
		log.info("----------curYear = "+curYear);
		List<TermSettingBean> specialtyList = getTermSettingService().queryTerymByYear(curYear);
		if(specialtyList == null){
			termSelectItem = new SelectItem[0];
		}else{
			termSelectItem = new SelectItem[specialtyList.size()];
			for(int i = 0 ; i < specialtyList.size() ; i++){
				TermSettingBean bean = specialtyList.get(i);
				termSelectItem[i] = new SelectItem(String.valueOf(bean.getId() ), bean.getTermYear()+"["+bean.getTermTypeName()+"]");
			}
		}
	}
	
	public void getpagedetail() {
		setItemCount(getClassScheduleService().getCount(CCPContants.getCurrUserId()));
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		classScheduleList = getClassScheduleService().pageQuery(start, pagesize,CCPContants.getCurrUserId());
		log.info("----------------------"+classScheduleList.size());
	}

	public void doDelete(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
		log.info("-----------------delete()----------------"+id);
		getClassScheduleService().delete(Integer.parseInt(id));
//		getpagedetail();
	}
	
	
	public String edit(){
		if(classScheduleBean.getName().equals("")){
			showMsg("name", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		if(classScheduleBean.getTermSettingId().equals("-1")){
			showMsg("termSettingId", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		log.info("---------------------------"+classScheduleBean);
		classScheduleBean.setTeacherId(CCPContants.getCurrUserId());
		if(classScheduleBean.getId() == 0)
			return save();
		return update();
	}
	public String save(){
		long begin = System.currentTimeMillis();
		boolean flag = getClassScheduleService().save(classScheduleBean);
		if(flag){
			//getpagedetail();
			long i = System.currentTimeMillis() - begin;
			log.info("-------------------"+i);
			return MAIN;
		}
		return "";
	}
	
	public String update(){
		boolean flag = getClassScheduleService().update(classScheduleBean);
		if(flag){
			//getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String detail(){
		log.info("======detail()=======");
		return "detail";
	}
	
	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		log.info("-----------------------------id = "+id);
		classScheduleBean =  (ClassScheduleBean)getClassScheduleService().findClassScheduleById(Integer.parseInt(id));
		return UPDATE;
	}
	
	
	public List<ClassScheduleBean> getClassScheduleList() {
		getpagedetail();
		return classScheduleList;
	}


	public void setClassScheduleList(List<ClassScheduleBean> classScheduleList) {
		this.classScheduleList = classScheduleList;
	}

	public ClassScheduleBean getClassScheduleBean() {
		if(classScheduleBean == null)
			classScheduleBean = new ClassScheduleBean();
		return classScheduleBean;
	}

	public void setClassScheduleBean(ClassScheduleBean classScheduleBean) {
		this.classScheduleBean = classScheduleBean;
	}

	public SelectItem[] getTermSelectItem() {
		return termSelectItem;
	}

	public void setTermSelectItem(SelectItem[] termSelectItem) {
		this.termSelectItem = termSelectItem;
	}
	
	
}
